package com.kurumi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kurumi.bo.PageParam;
import com.kurumi.mapper.StoreMapper;
import com.kurumi.pojo.Store;
import com.kurumi.service.PharmacyService;
import com.kurumi.util.AreaUtil;

@Transactional
@Service
public class PharmacyServiceImpl implements PharmacyService {

	@Autowired
	private StoreMapper storeMapper;

	@Transactional(readOnly = true)
	@Override
	public PageInfo<Map<String, Object>> getPharmacyPageInfo(HttpServletRequest request, PageParam pageParam) {
		PageHelper.startPage(pageParam.getPage(), pageParam.getLimit());
		Map<String, Object> params = new HashMap<>();

		String province = request.getParameter("province");
		if (StringUtils.isNotEmpty(province)) {
			params.put("province", AreaUtil.getDrugStoreProvinceName(province));
		}

		String city = request.getParameter("city");
		if (StringUtils.isNotEmpty(city)) {
			params.put("city", AreaUtil.getDrugStoreCityName(city));
		}

		String keyword = request.getParameter("keyword");
		if (StringUtils.isNotEmpty(keyword)) {
			params.put("keyword", keyword);
		}

		List<Map<String, Object>> orderDetailList = storeMapper.getPharmacyList(params);
		PageInfo<Map<String, Object>> objectPageInfo = new PageInfo<>(orderDetailList);
		return objectPageInfo;
	}

	@Transactional(readOnly = true)
	@Override
	public Store getPharmacyById(Integer pharmacyId) {

		return storeMapper.selectByPrimaryKey(pharmacyId);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void saveOrUpdateStore(Store store) {
		Integer id = store.getId();
		if (id == null) {
			storeMapper.insertSelective(store);
		} else {
			storeMapper.updateByPrimaryKey(store);
		}
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void delStoreById(Integer id) {
		
		storeMapper.deleteByPrimaryKey(id);
	}

//	@Transactional(readOnly = true)
//	@Override
//	public List<Map<String, Object>> getPrintDetailInfoByParams(HttpServletRequest request) {
//		Map<String,Object> params = new HashMap<String,Object>();
//		String start = request.getParameter("start");
//		if(StringUtils.isNotEmpty(start)) {
//			params.put("start", start);
//		}
//		
//		String end = request.getParameter("end");
//		if(StringUtils.isNotEmpty(end)) {
//			params.put("end", end);
//		}
//		
//		String ip = request.getParameter("ip");
//		if(StringUtils.isNotEmpty(ip)) {
//			params.put("ip", ip);
//		}
//		
//		String idCard = request.getParameter("idCard");
//		if(StringUtils.isNotEmpty(idCard)) {
//			params.put("idCard", idCard);
//		}
//		
//		return printMrOrderMapper.getPrintDetailInfoByParams(params);
//	}
//	

}
